namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0007. 整数反转")]
    public class No0007_Reverse : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(Reverse, 123, 321);
            Assert.TestExe(Reverse, -123, -321);
            Assert.TestExe(Reverse, 120, 21);
        }

        public int Reverse(int x)
        {
            int rev = 0;
            while (x != 0)
            {
                int pop = x % 10;
                x /= 10;
                if (rev > int.MaxValue / 10 || (rev == int.MaxValue / 10 && pop > 7))
                {
                    return 0;
                }
                if (rev < int.MinValue / 10 || (rev == int.MinValue / 10 && pop < -8))
                {
                    return 0;
                }
                rev = rev * 10 + pop;
            }
            return rev;
        }
    }
}
